import { Inject, Controller, Get, Post, Files, Fields } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service';
import { ApiBody, BodyContentType } from '@midwayjs/swagger';
import {copyFile} from 'fs/promises'
import { Material } from '../entity/material.entity';

@Controller('/api')
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  userService: UserService;

  @Get('/get_user')
  async getUser() {
    // const user = await this.userService.getUser({ uid });
    const user = this.ctx.getAttr('username');
    return { success: true, message: 'OK', data: user };
  }
  @Post("/upload")
  @ApiBody({
    description: '文件上传测试',
    contentType: BodyContentType.Multipart,
  })
  async upload(@Files() uploadFiles:Array<any> ){
    console.log(uploadFiles)
    for (let i = 0; i < uploadFiles.length; i++) {
        await copyFile(uploadFiles[i].data,
          '/home/xieweig/main/photo/'+uploadFiles[i].filename);
    }

    return uploadFiles;
  }
}
